﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RCMS.Web.WebControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:GoBackButton runat=server></{0}:GoBackButton>")]
    public class GoBackButton : WebControl
    {


        private Button _button;
        private int HistoryIndex;


        protected override void Render(HtmlTextWriter writer)
        {
            _button.RenderControl(writer);
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("返回")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? "返回" : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public override string CssClass
        {
            get
            {
                String s = (String)ViewState["CssClass"];
                return ((s == null) ? "ButtonLong" : s);
            }

            set
            {
                ViewState["CssClass"] = value;
            }
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            _button = new Button();
            _button.Text = this.Text;
            _button.CssClass = this.CssClass;
            _button.CausesValidation = false;
            this.Controls.Add(_button);
        }



        protected override void OnLoad(EventArgs e)
        {
            int index = 0;

            base.OnLoad(e);
            if (!this.Page.IsPostBack)
            {
                if (this.Page.Request.QueryString["h__index"] != null)
                {
                    int.TryParse(this.Page.Request.QueryString["h__index"].ToString(), out index);

                }

                HistoryIndex = index;
            }
            else
            {
                HistoryIndex = int.Parse(this.Page.Request.Form["History__Index"]);
            }
            HistoryIndex--;
            _button.Attributes["onclick"] = "if(window.history.length>0){window.history.go(" + HistoryIndex + ");}else{window.close()};return false;";

            this.Page.ClientScript.RegisterHiddenField("History__Index", HistoryIndex.ToString());

        }
    }
}
